import java.util.Random;
import java.util.concurrent.ExecutionException;

public class 数组求和 {
    public static int ans1 = 0;
    public static int ans2 = 0;
    public static void main(String[] args) throws InterruptedException, ExecutionException {
        long starttime = System.currentTimeMillis();
        int[] arr = new int[1000_0000];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            int tmp = random.nextInt(100) + 1;
            arr[i] = tmp;
            System.out.println(tmp);
        }

        Thread thread1 = new Thread(){
            @Override
            public void run() {
                //int ans1 = 0;
                for (int i = 0; i < arr.length; i = i + 2) {
                    ans1 += arr[i];
                }
                System.out.println("线程1：" + ans1);
            }
        };
        thread1.start();

        Thread thread2 = new Thread(){
            @Override
            public void run() {
                //int ans2 = 0;
                for (int i = 1; i < arr.length; i = i + 2) {
                    ans2 +=arr[i];
                }
                System.out.println("线程2：" + ans2);
            }
        };
        thread2.start();

        thread1.join();
        thread2.join();

        int num = ans1 + ans2;
        System.out.println("总和：" + num);

        long endtime = System.currentTimeMillis();
        long time = endtime - starttime;
        System.out.println("时间：" + time);
    }
}
